package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.NewsDao;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.News;
import com.chalk.service.NewsService;
import com.chalk.util.MpUtil;
import com.chalk.vo.NewsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-19
 */

@Service
public class NewsServiceImpl extends ServiceImpl<NewsDao, News> implements NewsService {

    @Autowired
    MpUtil<News> mpUtil;


    @Override
    public Page<News> getNewsForPage(Map<String, Object> map) throws ColumnNotEmptyException {
        String newsTitle=(String)map.get("newsTitle");
        EntityWrapper<News> wrapper=mpUtil.getWrapper();
        wrapper.like(StringUtils.isNoneBlank(newsTitle),"news_title",newsTitle);
        Page<News> newsPage=mpUtil.getPage(map);
        newsPage.setDescs(mpUtil.getDesc("create_time"));
        return selectPage(newsPage,wrapper);
    }

    @Override
    public Boolean saveNews(NewsVo newsVo) {
        newsVo.setCreateTime(LocalDateTime.now());
        News news=new News();
        BeanUtils.copyProperties(newsVo,news);
        return insert(news);
    }

    @Override
    public Boolean updateNews(NewsVo newsVo) {
        newsVo.setUpdateTime(LocalDateTime.now());
        News news=new News();
        BeanUtils.copyProperties(newsVo,news);
        return updateById(news);
    }

    @Override
    public Boolean deleteNews(Integer newsId) {
        News news=new News();
        news.setIsDelete(1);
        news.setNewsId(newsId);
        return updateById(news);
    }
}
